﻿using B.MES.Customer.Application.Application;
using B.MES.Customer.Application.IApplication;
using B.MES.Customer.Domain;
using B.MES.Customer.Domain.DTO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using System.Collections.Generic;
using Yitter.IdGenerator;

namespace B.MES.Customer.Read.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UsersController : ControllerBase
    {
        private readonly IRBACApplication _userApplication;

        public UsersController(IRBACApplication userApplication)
        {
            _userApplication = userApplication;
        }

        /// <summary>
        /// 根据用户名获取用户信息。
        /// </summary>
        /// <param name="loginInput">包含用户名和密码的登录信息。</param>
        /// <returns>根据用户名查询到的用户信息。</returns>
        [HttpPost]
        public IActionResult UserLogin(InputLogin loginInput)
        {
            if(loginInput == null)
            {
                return BadRequest("输入错误");
            }
            var logininfo = _userApplication.UserLogin(loginInput);
            if (logininfo == null)
            {
                return BadRequest("账号或者密码错误");
            }
            return Ok(logininfo);
        }

        /// <summary>
        /// Excel导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ExportToExcel()
        {
            try
            {
                var list = _userApplication.GetAll();
                var stream= new MemoryStream();
                stream.SaveAs(list); 
                stream.Seek(0, SeekOrigin.Begin);
                return new FileStreamResult(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                {
                    FileDownloadName = "用户列表"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }

        ///// <summary>
        ///// Excel导入
        ///// </summary>
        ///// <param name="file"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public IActionResult ImportExcel(IFormFile file)
        //{
        //    try
        //    {
        //        if (file == null || file.Length == 0)
        //        {
        //            return BadRequest("未选择文件或文件为空");
        //        }
 
        //        var formFile = Request.Form.Files[0];
        //        var fileName= formFile.FileName;
        //        var fileExtension = Path.GetExtension(formFile.FileName);
        //        if (fileExtension!= ".xlsx" && fileExtension!= ".xls")
        //        {
        //            return BadRequest("文件格式错误");
        //        }
        //        APIResult<string> apiResult = new APIResult<string>();
        //        using (var steam=new MemoryStream())
        //        {
        //            formFile.CopyTo(steam);
        //            steam.Position = 0;
        //            var list=steam.Query<ImportUser>();
        //            var users = list.Where(x => !string.IsNullOrEmpty(x.User_Id)).Select(x => new User
        //            {
        //                User_Id = x.User_Id,
        //                User_Name = x.User_Name,
        //                User_PassWord = x.User_PassWord,
        //                U_Name =x.U_Name,
        //                Job_number = x.Job_number,
        //                User_Phone = x.User_Phone,
        //                IsDelete = x.IsDelete
        //            }).ToList();
        //            //var users = list.Select(x => new User
        //            //{
        //            //    User_Id = x.User_Id,
        //            //    User_Name = x.User_Name,
        //            //    User_PassWord = x.User_PassWord,
        //            //    U_Name = x.U_Name,
        //            //    Job_number = x.Job_number,
        //            //    User_Phone = x.User_Phone,
        //            //    IsDelete = x.IsDelete
        //            //}).ToList();

        //            _userApplication.Add(users);
        //            _userApplication.SaveChanges();
        //        }
        //        apiResult.Success(fileName);
        //        return Ok(apiResult);
        //    }
        //    catch (Exception)
        //    {
        //        throw;
        //    }
        //}

    }
}
